import org.testng.TestListenerAdapter;
import org.testng.TestNG;
import until.StfDevicesOpearting;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;

import static config.commonconfig.testModelNum;

/**
 * @Description
 * @auther lileilei
 * @create 2020-03-16 15:12
 */

public class runsite {
    private String read(String path) {
        File file = new File(path);
        InputStream is = null;
        StringBuffer sb = new StringBuffer();
        try {
            is = new FileInputStream(file);
            int index = 0;
            byte[] b = new byte[1024];
            while ((index = is.read(b)) != -1) {
                sb.append(new String(b, 0, index));
            }
            return sb.toString();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static void main(String[] args) throws IOException {
        String num=System.getProperty("modelnum");
        Integer testnum=0;
        if (num!=null){
            testnum=Integer.parseInt(num);
        }else {
            testnum=testModelNum;
        }
        String templatePath = System.getProperty("user.dir") + File.separator + "suitetemplate";
        runsite run = new runsite();
        String template = run.read(templatePath);
        TestListenerAdapter tla = new TestListenerAdapter();
        TestNG testng = new TestNG();
        List<String> testFieldList = new ArrayList<>();
        StfDevicesOpearting stfDevicesOpearting = new StfDevicesOpearting();
        List<String> she = stfDevicesOpearting.getModelt();
        Integer port = 4723;
        List<Integer> portlist = new ArrayList<>();
        if (she.size() == testModelNum) {
            stfDevicesOpearting.getoneMobileUse(she);
            for (int i = 0; i < testModelNum; i++) {
                String url = stfDevicesOpearting.getmobileName(she.get(i));
                String cm = "adb connect " + url;
                String adbdevivicecmd = "adb devices";
                String phonename = "";
                String path = System.getProperty("user.dir") + File.separator + "test" + i + ".xml";
                BufferedWriter output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(path)),
                        "UTF-8"));
                template = template.replaceFirst("suitename", "test" + i)
                        .replaceFirst("portid", String.valueOf(port + 1))
                        .replaceFirst("nametest", "test" + i)
                        .replaceFirst("udidid", phonename);

                output.write(template);
                output.flush();
                output.close();
                testFieldList.add("test" + i + ".xml");
                portlist.add(port + 1);
            }
            stfDevicesOpearting.returnMobileUse(she);
            for (Integer i : portlist) {
                //todo  设备测试完毕后，端口暂停
            }
            testng.setTestSuites(testFieldList);
            testng.run();
            testng.setSuiteThreadPoolSize(testModelNum);
        }


    }
}
